﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace W1
{
    public partial class ZhuCeForm : Form
    {
        DataTable dt = new DataTable();
        bool result = false;//判断值
        bool restlts;

        public ZhuCeForm()
        {
            InitializeComponent();
        }

        //切换窗体，调用窗体
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            //切换窗体（调用窗体）
            Form4 form4 = new Form4();
            form4.Show();
            Hide();//隐藏
        }


        //删除方法
        private void butDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string content = txtSelect.Text;
                //非空验证
                if (content == "")
                {
                    MessageBox.Show("搜索框不能为空", "输入提示：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    DialogResult restlt = MessageBox.Show("确定删除该学生信息？", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (restlt == DialogResult.OK)
                    {
                        string sql = string.Format("delete from ZhuCe where StudentID='{0}'", content);
                        restlts = DBHelper.ExecuteNonQuery(sql);
                        if (restlts)
                        {
                            MessageBox.Show("删除成功！！");
                            return;
                        }
                        else
                        {
                            MessageBox.Show("删除失败！！");
                            return;
                        }

                    }
                    return;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
             
        }

        //添加方法
        private void butAdd_Click(object sender, EventArgs e)
        {
            
            panelExit.Visible = false;//添加数据时关闭panel容器，并显示编辑信息板块
           
        }

        //查询方法
        private void butSelect_Click(object sender, EventArgs e)
        {
            try
            {
                string content = txtSelect.Text;
                //非空验证
                if (content=="")
                {
                    MessageBox.Show("搜索框不能为空","输入提示：",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }
                result = true;
                //不存在验证
                //1、sql语句  2、DBHelper调用
                if (result == true)
                {
                    
                    string sql = string.Format("select Acadeyear,Grade,Studentsta,SchoolSta,RegisterSta from ZhuCe where  StudentID='{0}'", content);
                    dt = DBHelper.SelectByAdapter(sql);
                    if (dt.Rows.Count>0)
                    {
                        ShowInfo(0);
                        panelExit.Visible = false;
                        txtSelect.SelectAll();
                        this.txtSelect.Focus();
                        return;
                    }
                    else
                    {
                        panelExit.Visible = true;
                        MessageBox.Show("查无此学号", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtSelect.SelectAll();
                        this.txtSelect.Focus();
                    }
                }
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }


        //数据加载和显示功能 方法
        public void ShowInfo(int i)
        {
            cmbYear.Text = dt.Rows[i]["Acadeyear"].ToString();
            cmbGrade.Text = dt.Rows[i]["Grade"].ToString();
            cmbXueLi.Text = dt.Rows[i]["Studentsta"].ToString();
            cmbZaiXiao.Text = dt.Rows[i]["SchoolSta"].ToString();
            cmbZhuCe.Text = dt.Rows[i]["RegisterSta"].ToString();
        }
        
        //按下添加按钮后，再单击确定按钮是事件
        private void butQueDing_Click_1(object sender, EventArgs e)
        {
            string Year = cmbYear.Text.Trim();
            string Grade = cmbGrade.Text.Trim();
            string XueLi = cmbXueLi.Text.Trim();
            string ZaiXiao = cmbZaiXiao.Text.Trim();
            string ZhuCe = cmbZhuCe.Text.Trim();
            string sql = string.Format("insert into ZhuCe(Acadeyear,Grade,Studentsta,SchoolSta,RegisterSta) " +
                "values('{0}','{1}','{2}','{3}','{4}')", Year, Grade, XueLi, ZaiXiao, ZhuCe);
            restlts = DBHelper.ExecuteNonQuery(sql);
            if (restlts)
            {
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("添加失败");
            }
            panelExit.Visible=true;
        }

        //取消按钮
        private void butQuXiao_Click_1(object sender, EventArgs e)
        {

            panelExit.Visible = true;//关闭面板，打开panel容器
            //取消方法
            //将所有输入框中的数据清零
            cmbYear.Text = "";
            cmbGrade.Text = "";
            cmbXueLi.Text = "";
            cmbZaiXiao.Text = "";
            cmbZhuCe.Text = "";
            
        }

        //修改按钮
        private void butUpdate_Click(object sender, EventArgs e)
        {

            string content = txtSelect.Text;
            //非空验证
            if (content == "")
            {
                MessageBox.Show("搜索框不能为空", "输入提示：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                DialogResult restlt = MessageBox.Show("确定更改该学生信息？", "提示", MessageBoxButtons.OKCancel);
                if (restlt == DialogResult.OK)
                {
                    string sql = string.Format("update ZhuCe set Acadeyear='{0}',Grade='{1}',Studentsta='{2}',SchoolSta='{3}',RegisterSta='{4}' where StuentID='{5}'",content);
                    restlts = DBHelper.ExecuteNonQuery(sql);
                    if (restlts)
                    {
                        MessageBox.Show("更改成功！！");
                    }
                    else
                    {
                        MessageBox.Show("更改失败！！");
                    }
                }
             }

        }

        private void ZhuCeForm_Load(object sender, EventArgs e)
        {

        }
    }
}
